const Rize = require('rize');
const rize = new Rize({headless: false});
const url = 'https://www.meizu.com/index.html';

class Crawler {
    constructor({url}) {
        this.url = url;
        this.init()
    }

    async init() {
        await rize.goto(this.url, {waitUntil: 'networkidle2'})
    }

    sleep(item) {
        return new Promise(resolve => {
            setTimeout(resolve, item)
        })
    }

    async theData(fn, fnc) {
        let dataUrl = '';
        return new Promise(async (resolve) => {
            await rize.execute(async (browser, page) => {
                let data = await fn(page);
                // console.log(data)
                if (data) {
                    dataUrl = data
                }
                resolve(data)
            })
        })
    }

    async evaluateWith(fnc,name) {

        const byExpr = await rize.evaluateWithReturn(fnc,name);
        // console.log(byExpr)
        return byExpr
    }

    async PageDown({fn, fnc} = {}) {
        let i = 0;
        let self = this;
        let itemr = null;
        await self.sleep(3000)
        return new Promise(async (resolve) => {
            (async function item() {
                if (i > 10) {
                    if (fn) {
                        resolve(self.theData(fn));
                    } else {
                        resolve(self.evaluateWith(fnc))
                    }
                    console.log('翻页完成')
                    return;
                }

                itemr = setTimeout(async () => {

                    i++
                    await rize.keyDown('PageDown');
                    item();
                }, 1550)
            })()
        })
    }

    async newPage({name,obj,url}) {
        await rize.newPage(name, obj);
        await rize.goto(url);
        await this.PageDown()
    }
}

let req = new Crawler({url})

req.evaluateWith((name) => {
    console.log(name)
},'lxl')




















